package com.ts.api.http.org.base;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyLiYongBeiJingData;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeLiYongBeiJing2;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 利勇信息流
 *
 * @author WJB
 */
public class ApiOrgPhoneMd5LiYong2 implements ApiOrgPhoneMd5ConvertHandler {
    public final String key;
    public final String channel;

    public ApiOrgPhoneMd5LiYong2(String key, String channel) {
        this.key = key;
        this.channel = channel;
    }

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgDataExchangeLiYongBeiJing2 orgDataExchangeLiYongBeiJing = new OrgDataExchangeLiYongBeiJing2();
        orgDataExchangeLiYongBeiJing.setMobile_md5(apiDataExchangeParameter.getPhoneMd5());
        orgDataExchangeLiYongBeiJing.setKey(key);
        orgDataExchangeLiYongBeiJing.setChannel(channel);
        //  不能包含市
        String city = apiDataExchangeParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            orgDataExchangeLiYongBeiJing.setCity(city);
        }
        return orgDataExchangeLiYongBeiJing;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("errNum"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);

        OrgApplyLiYongBeiJingData orgApplyLiYongBeiJingData = new OrgApplyLiYongBeiJingData();

        orgApplyLiYongBeiJingData.setChannel("axfq");
        orgApplyLiYongBeiJingData.setName(apiApplyParameter.getUserName());
        orgApplyLiYongBeiJingData.setMobile(apiApplyParameter.getPhone());
        orgApplyLiYongBeiJingData.setKey(key);

        //  不能包含市
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            orgApplyLiYongBeiJingData.setCity(city);
        }

        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  贷款额度
        orgApplyLiYongBeiJingData.setMoney(apiFieldConverter.getLoanAmountInt());
        //  性别
        String sex = apiApplyParameter.getSex();
            String sexChoose = ChooseEq.create()
                    .when(sex, "0", "男")
                    .when(sex, "1", "女")
                    .end();
        orgApplyLiYongBeiJingData.setSex(sexChoose);

        //  年龄
        orgApplyLiYongBeiJingData.setAge(apiFieldConverter.getAge());

        //  学历
        orgApplyLiYongBeiJingData.setXueli(apiFieldConverter.getEducationAttainmentText());
        //  房产情况
        orgApplyLiYongBeiJingData.setHas_house(apiFieldConverter.hasRealEstateInfoToStr());
        //  车产
        orgApplyLiYongBeiJingData.setHas_car(apiFieldConverter.hasCarInfoToStr());
        //  公积金
        orgApplyLiYongBeiJingData.setHas_gongjijin(apiFieldConverter.hasProvidentFundToStr());
        //  保单
        orgApplyLiYongBeiJingData.setHas_baodan(apiFieldConverter.hasInsurancePolicyToStr());
        //  社保
        orgApplyLiYongBeiJingData.setHas_shebao(apiFieldConverter.hasSocialSecurityToStr());
        //  营业执照
        orgApplyLiYongBeiJingData.setHas_company(apiFieldConverter.hasBizLicenseYearToStr());

        StringBuilder stringBuilder = new StringBuilder();
        //  备注信息
        //  信用记录-逾期记录
        if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getOverdue(), "1", "无逾期")
                    .when(apiApplyParameter.getOverdue(), "2", "有逾期")
                    .when(apiApplyParameter.getOverdue(), "3", "一年以内无逾期")
                    .end();
            stringBuilder.append("信用记录：").append(text).append(";").append("\n");
        }

        //  芝麻分
        if (ValidParam.isNotEmpty(apiApplyParameter.getSesameCredit())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getSesameCredit(), "1", "600分以下")
                    .when(apiApplyParameter.getSesameCredit(), "2", "600~649分")
                    .when(apiApplyParameter.getSesameCredit(), "3", "650~699分")
                    .when(apiApplyParameter.getSesameCredit(), "4", "700分以上")
                    .end();
            stringBuilder.append("芝麻分：").append(text).append(";").append("\n");
        }

        //  职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", "上班族")
                    .when(apiApplyParameter.getCareer(), "2", "自由职业")
                    .when(apiApplyParameter.getCareer(), "3", "企业主（有营业执照）")
                    .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
                    .end();
            stringBuilder.append("职业：").append(text).append(";").append("\n");
        }
        //  收入形式
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发打卡")
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金收入")
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", "个人转账")
                    .end();
            stringBuilder.append("收入形式：").append(text).append(";").append("\n");
        }

        //  客户类型
        String career = apiApplyParameter.getCareer();
        if (ValidParam.isEmpty(career)) {
            //  无数据为个人用户
            stringBuilder.append("客户类型：").append("个人").append(";").append("\n");
        } else {
            String text = ChooseEq.create()
                    //  如果是企业就为企业用户
                    .when(career, "3", "企业")
                    //  个人用户
                    .end("个人");
            stringBuilder.append("客户类型：").append(text).append(";").append("\n");
        }
        //  银行代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "是")
                    .end("否");
            stringBuilder.append("银行代发：").append(text).append(";").append("\n");
        }

        orgApplyLiYongBeiJingData.setRemark(stringBuilder.toString());
        return orgApplyLiYongBeiJingData;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("errNum"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
